package org.example.controller;

import org.example.domain.User;
import org.example.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 控制器层调用业务层 传递参数
 */
@RestController// RestController=@Controller+@ResponseBody 所有返回数据都为json格式
public class UserController {
    //依赖注入UserService
    @Autowired
    private IUserService userService;

    /**
     *
     * @return
     */
    @RequestMapping("/findAll")
    public List<User> findAll(){
        List<User> users = userService.findAll();
        return users;
    }

    /**
     * 新增
     */
    @RequestMapping("/save")
    //(@RequestBody  接收post请求体中的数据
    public String save(@RequestBody User user){
        userService.save(user);
        return "新增成功";
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    //(@RequestBody  接收post请求体中的数据
    public String update(@RequestBody User user){
        userService.update(user);
        return "修改成功";
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //(@RequestBody  接收post请求体中的数据
    public String delete(Integer id){
        userService.delete(id);
        return "删除成功";
    }
}
